﻿Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Xml.Linq
Imports Exp.Model
Imports Exp.DATA_Service
Imports Exp.DATA_Service.Exp.DATA_Service

Public Class ApproveBooking
    Inherits BasePage

    Const MENUNAME As String = "APPRBOOK"

    Const SaveSuccess As String = "Save completed."
    Const SaveError As String = "Save error !"
    Const DeleteSuccess As String = "Delete completed."
    Const DeleteError As String = "Delete not complete"
    Private _User As REM_User

    Private Property user() As REM_User
        Get
            If Session("USER") Is Nothing Then Response.Redirect("~/login.aspx")
            Return _User
        End Get
        Set(value As REM_User)
            _User = value
        End Set
    End Property

    Dim ds As List(Of EXP_Get_Booking)
    Public Property Datasource() As List(Of EXP_Get_Booking)
        Get
            If ds Is Nothing Then
                ds = New List(Of EXP_Get_Booking)
            End If

            Return ds
        End Get
        Set(ByVal value As List(Of EXP_Get_Booking))
            ds = value
        End Set
    End Property

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            If Session("USER") Is Nothing Then
                Response.Redirect("~/login.aspx")
            Else
                user = CType(Session("USER"), REM_User)

                Dim objUSV As New Exp.DATA_Service.User_Service
                If Not objUSV.isHasRight(user.User_ID, MENUNAME) Then
                    Session("USER") = Nothing
                    Session.Clear()
                    Response.Redirect("~/login.aspx")
                End If

                MultiView1.ActiveViewIndex = 0

                LoadData()
                BindControl()

                BindDropdown()
            End If
        End If

    End Sub

    Protected Sub btnSearchView0_Click(sender As Object, e As EventArgs) Handles btnSearchView0.Click
        LoadData()
        BindControl()
    End Sub

    Private Sub BindControl()
        grdView0.DataSource = Datasource
        grdView0.DataBind()
    End Sub

    Private Sub LoadData()
        Dim cnn As New Booking_Service()
        ds = cnn.Search(GetDataUtil.GetNullableInteger(txtBookIdView0.Text), GetDataUtil.GetNullableString(txtUsernameView0.Text), _
                        GetDataUtil.GetNullableString(txtFNameView0.Text), GetDataUtil.GetNullableString(txtLNameView0.Text), _
                        GetDataUtil.GetNullableDateTime(txtCheckinDateView0.Text), GetDataUtil.GetNullableDateTime(txtCheckoutDateView0.Text), _
                        GetDataUtil.GetNullableInteger(ddlCountryView0.SelectedValue), GetDataUtil.GetNullableInteger(ddlBrandView0.SelectedValue), _
                        GetDataUtil.GetNullableInteger(ddlResortView0.SelectedValue), GetDataUtil.GetNullableString(txtLocationView0.Text), _
                        GetDataUtil.GetNullableString(ddl_StatusSearch.Text))
        ds = ds.OrderByDescending(Function(p) p.Booking_ID).ToList
    End Sub

    Private Sub BindDropdown()
        Dim cty As New Country_Service
        ddlCountryView0.DataSource = cty.GetAll()
        ddlCountryView0.DataTextField = "CountryName"
        ddlCountryView0.DataValueField = "Country_ID"
        ddlCountryView0.DataBind()

        Dim brn As New Brand_Service
        ddlBrandView0.DataSource = brn.GetAll()
        ddlBrandView0.DataTextField = "BrandName"
        ddlBrandView0.DataValueField = "Brand_ID"
        ddlBrandView0.DataBind()

        Dim rst As New Resort_service
        ddlResortView0.DataSource = rst.GetAll()
        ddlResortView0.DataTextField = "ResortName"
        ddlResortView0.DataValueField = "Resort_ID"
        ddlResortView0.DataBind()
    End Sub

    Protected Sub grdView0_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles grdView0.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim data As EXP_Get_Booking = e.Row.DataItem
            If data.Status.ToUpper.Substring(0, 4) = "APPR" OrElse data.Status.ToUpper = "REJECT" Then
                Dim lb As LinkButton = e.Row.FindControl("lnkApprove")
                lb.Visible = False
            End If
        End If
    End Sub

    Protected Sub grdView0_RowCommand(sender As Object, e As GridViewCommandEventArgs) Handles grdView0.RowCommand

        MultiView1.ActiveViewIndex = 1

        hdfBookID.Value = e.CommandArgument
        LoadView2(Convert.ToInt32(e.CommandArgument))

        If e.CommandName.ToUpper = "APPROVE" Then
            BtnUpdateStatus.Visible = True
            ddlStatus.Enabled = True
            ddlStatus.Items.Clear()
            ddlStatus.Items.Add(New ListItem("Approve", "APPROVE"))
            ddlStatus.Items.Add(New ListItem("Reject", "REJECT"))

        ElseIf e.CommandName.ToUpper = "DETAIL" Then
            BtnUpdateStatus.Visible = False
            ddlStatus.Enabled = False
        End If
    End Sub

    Private Sub LoadView2(ByVal BookID As Integer)

        Dim bkCtx As New Booking_Service()
        Dim res As EXP_Get_Booking = bkCtx.Search(BookID, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing).FirstOrDefault
        lblBookIDView1.Text = res.Booking_ID
        lblResortNameView1.Text = res.ResortName
        lblUnitNoView1.Text = res.UnitName
        lblCurrencyView1.Text = res.Currency_Name
        lblRateView1.Text = res.RatePrice.Value.ToString("#,###")
        lblPoolPointView1.Text = res.PointPerNight.ToString("#,###")
        lblCountryView1.Text = res.CountryName
        lblNightView1.Text = res.Night

        Dim usrCtx As New User_Service()
        Dim usr As REM_User = usrCtx.GetByID(res.Requester_ID)
        lblFNameView1.Text = usr.FirstName
        lblLNameView1.Text = usr.LastName
        lblPhoneView1.Text = usr.HomePhone
        lblMobileView1.Text = usr.MobilePhone
        lblEmailView1.Text = usr.Email
        lblJoinDate.Text = usr.JoinDate.Value.ToString("dd MMM yyyy")
        lblExpireDate.Text = usr.ExpireDate.Value.ToString("dd MMM yyyy")


        If res.Status.ToUpper <> "REQUEST" AndAlso res.Status IsNot Nothing AndAlso res.Status <> "" Then
            ddlStatus.Items.Clear()
            ddlStatus.Items.Add(New ListItem("Approve", "APPROVE"))
            ddlStatus.Items.Add(New ListItem("Reject", "REJECT"))
            ddlStatus.SelectedValue = res.Status
        Else
            ddlStatus.Items.Clear()
            ddlStatus.Items.Add(New ListItem("Request", "REQUEST"))
        End If

    End Sub

    Protected Sub ddlStatus_SelectedIndexChanged(sender As Object, e As EventArgs)
        Dim b As Boolean = (ddlStatus.SelectedValue = "REJECT")
        divReject.Visible = b
        rqRejectReason.Enabled = b
    End Sub

    Protected Sub btnBack_Click(sender As Object, e As EventArgs)
        MultiView1.ActiveViewIndex = 0
        txtRejectReason.Text = ""
    End Sub

    Protected Sub BtnUpdateStatus_Click(sender As Object, e As EventArgs)
        Dim usr As REM_User = Session("USER")
        Dim bkCtx As New Booking_Service
        Try
            bkCtx.UpdateStatus(Convert.ToInt32(hdfBookID.Value), ddlStatus.SelectedValue, usr.User_ID, txtRejectReason.Text)
            JavaScript.AlertMessage(Me.Page, SaveSuccess)
        Catch ex As Exception
            JavaScript.AlertMessage(Me.Page, SaveError & " : " & ex.Message)
        End Try
        txtRejectReason.Text = ""
        LoadData()
        BindControl()

        MultiView1.ActiveViewIndex = 0
    End Sub

    Protected Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btn_search.Click
        LoadData()
        BindControl()
    End Sub

    Protected Sub btnClear_Click(sender As Object, e As EventArgs) Handles btn_Clear.Click
        'Clear Control 'add By pitza
        txtBookIdView0.Text = ""
        txtFNameView0.Text = ""
        txtLNameView0.Text = ""
        txtCheckinDateView0.Text = ""
        txtCheckoutDateView0.Text = ""
        ddlCountryView0.Text = ""
        ddlBrandView0.Text = ""
        ddlResortView0.Text = ""
        txtLocationView0.Text = ""
        ddl_StatusSearch.Text = ""
        LoadData()
        BindControl()
    End Sub

    Private Sub grdView0_Sorting(sender As Object, e As GridViewSortEventArgs) Handles grdView0.Sorting
        LoadData()
        BindControl()
    End Sub
End Class